package com.xinqi.modules.chat.chat.group.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xinqi.common.base.user.Account;
import com.xinqi.common.core.constant.MQConstants;
import com.xinqi.common.core.enums.RocketMqEnum;
import com.xinqi.common.core.enums.RocketMqRongyunEnum;
import com.xinqi.common.rocketmq.dto.ChatGroupMessageBody;
import com.xinqi.common.rocketmq.dto.MqMessageDTO;
import com.xinqi.common.rocketmq.event.ProducerEvent;
import com.xinqi.modules.chat.chat.group.domain.ChatGroupEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 融云群组操作MQ消息发送
 *
 * @Author：yyzhen
 * @Package：com.xinqi.modules.chat.chat.group.service.impl
 * @Project：XingqiClass-Cloud-Plus
 * @name：ChatGroupMQService
 * @Date：2023/7/26 11:26
 * @Filename：ChatGroupMQService
 */
@Log4j2
@Validated
@Service("chatGroupMQService")
@RequiredArgsConstructor
public class ChatGroupMQService {

    /**
     * 融云--账号注销
     *
     * @param userId
     */
    @Async("threadPoolTaskExecutor")
    public void accountCancel(Long userId) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_USER_DEACTIVATE.getCode());
        body.setUserId(userId);
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.ACCOUNT_MESSAGE_KEY + userId);
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--禁言指定群成员
     *
     * @param group
     * @param userIds
     */
    @Async("threadPoolTaskExecutor")
    public void sendProhibitionMessage(ChatGroupEntity group, List<String> userIds) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GAG_ADD.getCode());
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        body.setUserIds(userIds.toArray(new String[userIds.size()]));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--禁言指定群成员
     *
     * @param group
     * @param userId
     */
    @Async("threadPoolTaskExecutor")
    public void sendProhibitionMessage(ChatGroupEntity group, String userId) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GAG_ADD.getCode());
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        body.setUserIds(new String[]{userId});
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--取消指定群成员禁言
     *
     * @param group
     * @param userIds
     */
    @Async("threadPoolTaskExecutor")
    public void sendCancelProhibitionMessage(ChatGroupEntity group, List<String> userIds) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GAG_REMOVE.getCode());
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        body.setUserIds(userIds.toArray(new String[userIds.size()]));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--取消指定群成员禁言
     *
     * @param group
     * @param userId
     */
    @Async("threadPoolTaskExecutor")
    public void sendCancelProhibitionMessage(ChatGroupEntity group, String userId) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GAG_REMOVE.getCode());
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        body.setUserIds(new String[]{userId});
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--创建群聊
     *
     * @param group
     * @param userId
     */
    @Async("threadPoolTaskExecutor")
    public void sendCreateMessage(ChatGroupEntity group, Long userId) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_CREATE.getCode());
        body.setUserId(userId);
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--加入群聊
     *
     * @param group
     * @param userIds
     */
    @Async("threadPoolTaskExecutor")
    public void sendAddMessage(ChatGroupEntity group, List<String> userIds) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_JOIN.getCode());
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        body.setUserIds(userIds.toArray(new String[userIds.size()]));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--加入群聊
     *
     * @param group
     * @param userId
     */
    @Async("threadPoolTaskExecutor")
    public void sendAddMessage(ChatGroupEntity group, String userId) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_JOIN.getCode());
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        body.setUserIds(new String[]{userId});
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--退出群聊
     *
     * @param group
     * @param userIds
     */
    @Async("threadPoolTaskExecutor")
    public void sendQuitMessage(ChatGroupEntity group, List<String> userIds) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_QUIT.getCode());
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        body.setUserIds(userIds.toArray(new String[userIds.size()]));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--退出群聊
     *
     * @param group
     * @param userId
     */
    @Async("threadPoolTaskExecutor")
    public void sendQuitMessage(ChatGroupEntity group, String userId) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_QUIT.getCode());
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        body.setUserIds(new String[]{userId});
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--更新群组名称
     *
     * @param user
     * @param group
     */
    @Async("threadPoolTaskExecutor")
    public void sendRenameMessage(Account user, ChatGroupEntity group) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_UPDATE.getCode());
        body.setUserId(user.getId());
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 融云--解散群聊
     *
     * @param group
     */
    @Async("threadPoolTaskExecutor")
    public void sendDismissMessage(ChatGroupEntity group) {
        ChatGroupMessageBody body = new ChatGroupMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_GROUP_DISMISS.getCode());
        body.setUserId(group.getUserId());
        body.setGroupId(group.getGroupId());
        body.setGroupName(group.getGroupName());
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_OPERATION.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.GROUP_MESSAGE_KEY + group.getGroupId());
        ProducerEvent.producerFifoMessage(message);
    }

}
